{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 03. Objects of arbitrary shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fdtd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "fdtd.set_backend(\"numpy\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Grid Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid = fdtd.Grid(\n",
    "    shape = (300, 300, 1), # 25um x 15um x 1 (grid_spacing) --> 2D FDTD\n",
    "    grid_spacing = 1e-7,\n",
    "    permittivity = 1,\n",
    ")\n",
    "\n",
    "grid[50:250, 50, 0] = fdtd.LineSource(\n",
    "    period = 1550e-9 / (3e8), name=\"source\"\n",
    ")\n",
    "\n",
    "grid[50:250, 250, 0] = fdtd.LineDetector(name=\"detector\")\n",
    "\n",
    "grid[0:10, :, :] = fdtd.PML(name=\"pml_xlow\")\n",
    "grid[-10:, :, :] = fdtd.PML(name=\"pml_xhigh\")\n",
    "grid[:, 0:10, :] = fdtd.PML(name=\"pml_ylow\")\n",
    "grid[:, -10:, :] = fdtd.PML(name=\"pml_yhigh\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Circular Object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "refractive_index = 1.7\n",
    "x = y = np.linspace(-1,1,100)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "circle_mask = X**2 + Y**2 < 1\n",
    "permittivity = np.ones((100,100,1))\n",
    "permittivity += circle_mask[:,:,None]*(refractive_index**2 - 1)\n",
    "grid[170:270, 100:200, 0] = fdtd.Object(permittivity=permittivity, name=\"object\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run Simulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid.run(total_time=500)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Even though visualization of a circular object is not implemented (for now), one can clearly see the focussing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid.visualize(z=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid.object.inverse_permittivity.min()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
